using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;

namespace Hoshimi
{
    public class CollectorBot_MoveToAZNState : State
	{
		#region Singleton

		// Fields
		protected static CollectorBot_MoveToAZNState instance;

		// Constructor
		protected CollectorBot_MoveToAZNState() : base() { }

		// Methods
		public static CollectorBot_MoveToAZNState Instance()
		{
			// Uses "Lazy initialization"
			if( instance == null ) 
				instance = new CollectorBot_MoveToAZNState();

			return instance;
		}

		#endregion

		public override bool Play( Unit unit ) 
        {
			CollectorBot bot = (CollectorBot) unit;

			bot.SetName("MoveToAZN");

			if ( bot.State == NanoBotState.Moving ) 
			{
                if (bot.IsFull())
                {
                    bot.StopIfMoving();
                    unit.myStateMachine.ChangeState( CollectorBot_MoveToHoshimiState.Instance() );
                    return true;
                }
			}

			// We are not on the AZN point
			if ( !bot.IsCollectPossible() ) 
			{
				// The needle is full or dead or taken by ennemy ? We stop
				if ( bot.IsMyNeedleTakenByEnemy() || bot.IsMyNeedleFullOrDead() ) 
				{
					unit.myStateMachine.ChangeState( CollectorBot_MoveToHoshimiState.Instance() );
					return true;
				}

				bot.MoveToAZN();
				return true;
			}

			bot.StopIfMoving();

			// We cant give order to the bot. We stop
			if ( bot.State != NanoBotState.WaitingOrders ) 
			{
				return true;
			}

			// The bot is on the AZN point : we order to collect
			unit.myStateMachine.ChangeState( CollectorBot_CollectAZNState.Instance() );
			return false;
		}
	}
}
